{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using gpu device 0: GeForce GTX 1070 (CNMeM is enabled with initial size: 75.0% of memory, cuDNN 5005)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import theano\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/gluca/SoftwareProjects/Thesis/MDBN/notebooks'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "file='../data/AML/Patient_MutatedGenes_somatic.NoNorm.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "patient = dict()\n",
    "p = re.compile('TCGA-[^-]+-[0-9]+')\n",
    "with open(file) as f:\n",
    "    record = f.readline().split('\\t')\n",
    "    while True:\n",
    "        record = f.readline().split('\\t')\n",
    "        if record[0] == '':\n",
    "            break;\n",
    "        m = p.match(record[0])\n",
    "        patient_id=m.group()\n",
    "        if not patient_id in patient:\n",
    "            patient[patient_id] = dict()\n",
    "        if record[1] != 'sample':\n",
    "            patient[patient_id][record[1]] = float(record[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Number of patients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "197"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(patient.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Number of genes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1890\n"
     ]
    }
   ],
   "source": [
    "genes=[]\n",
    "for k,v in patient.iteritems():\n",
    "    for gene in v.keys():\n",
    "        if not gene in genes:\n",
    "            genes.append(gene)\n",
    "print(len(genes))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "patient_list = []\n",
    "with open('../data/AML/pat_id.txt','r') as f:\n",
    "    f.readline() # skip the header\n",
    "    while True:\n",
    "        record = f.readline().strip()\n",
    "        if record == '':\n",
    "            break;\n",
    "        patient_list.append(record)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "file2 = '../data/AML/AML_somatic_mutations_table2.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with open(file2,'w') as out:\n",
    "    out.write('ID')\n",
    "    for patient_id in patient_list:\n",
    "        out.write('\\t')\n",
    "        out.write(patient_id)\n",
    "    out.write('\\n')\n",
    "    for gene in genes:\n",
    "        out.write(gene)\n",
    "        for patient_id in patient_list:\n",
    "            out.write('\\t')\n",
    "            if gene in patient[patient_id]:\n",
    "                out.write(str(patient[patient_id][gene]))\n",
    "            else:\n",
    "                out.write(\"0.0\")\n",
    "        out.write('\\n')\n",
    "    out.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
